use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json;
use std::fs;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Record {
    name: String,
    position: String,
    #[serde(rename = "DOB")]
    dob: String,
    nationality: String,
    #[serde(rename = "Kit Number")]
    kit: u8,
}

pub fn to_json_file(input: &str, output: &str) -> Result<()> {
    // let header = opts.header;
    // let delemiter = opts.delemiter;
    // csv_to_json(input, output, header, delemiter)?;
    let mut rdr = csv::Reader::from_path(input)?;
    let mut ret = Vec::with_capacity(128);
    for record in rdr.deserialize() {
        let record: Record = record?;
        ret.push(serde_json::to_string(&record)?);
        println!("{:?}", record);
    }
    let json = serde_json::to_string_pretty(&ret)?;
    fs::write(output, json)?;
    Ok(())
}
